import request from "./request.js" // 请求方法
import wx from "weixin-js-sdk"; // 引入微信js-sdk
import { isWxMiniProgram } from "./common-func.js";
/** 微信小程序人脸识别  */
export function wechatMiniFaceDetect() {
	if (isWxMiniProgram()) {
		// wx.miniProgram.navigateTo({url:'/onlineSign/pages/verifyPage/verifyPage?bizToken=1F718783-986B-4D8E-B6FF-106E7E46F98D'})
		// return
		let jsondata = {
			userName: "杨土发",
			idNo: "440823199109044965"
		}
		request({
			url: '/MIPSS_APP_CL4/rest/face/getFaceTokenDemo.action',
			method: 'GET',
			data: jsondata
		}).then(res => {
			console.log(res)
			let data = res.data
			wx.miniProgram.navigateTo({url:'/onlineSign/pages/verifyPage/verifyPage?bizToken='+data})
		})
	} else{
		uni.showToast({
			title: '请在小程序中访问，才能正常使用人脸识别。',
			icon: 'none',
			duration: 2000
		})			
	}
}

/** 微信小程序拍照 */
var wechatMiniChooseImageCallBackFun;
var wechatMiniChooseImageCallBackFunBatchSeq = "";
export function wechatMiniChooseImage(obj, _this) {
	// if (isWxMiniProgram()) {
		let jsondata = {}
		const {
			sizeType = ["original", "compressed"],
			sourceType = ["album", "camera"],
			count = 1,
			compressed = true, // 是否压缩
			callBackFun = () => {}
		} = obj;
		_this.wechatMiniChooseImageCallBackFunBatchSeq = Math.random().toString(36).substr(2)
		// 在 H5 中监听显示和隐藏事件
		document.addEventListener('visibilitychange', function() {
			if (document.visibilityState === 'visible') {
				// 处理 onShow 事件
				console.log('H5 Page onShow');
			} else {
				// 处理 onHide 事件
				console.log('H5 Page onHide');
			}
		});
		wx.miniProgram.navigateTo({url: '/onlineSign/pages/uploadImagePage/uploadImagePage?compressed='+compressed+'&sourceType='+sourceType+'&count='+count+'&batchSeq='+wechatMiniChooseImageCallBackFunBatchSeq});
		return
		request({
			url: '/MIPSS_APP_CL4/rest/face/getFaceTokenDemo.action',
			method: 'POST',
			data: jsondata
		}).then(res => {
			let data = res.data
			wechatMiniChooseImageCallBackFunBatchSeq = data.data.batchSeq
			wechatMiniChooseImageCallBackFun = callBackFun
			if(!handleVisibilityChange()){
				document.addEventListener("visibilitychange", () => {
					wechatMiniChooseImageCallBackFunBatchSeq = ""
					wechatMiniChooseImageCallBackFun(data.data)
				})
			}
			wx.miniProgram.navigateTo({url: '/onlineSign/pages/uploadImagePage/uploadImagePage?compressed='+compressed+'&sourceType='+sourceType+'&count='+count+'&unionId='+data.data.unionId+'&sys='+data.data.sys+'&batchSeq='+data.data.batchSeq+'&sign='+data.data.sign});
		})
	// } else{
	// 	uni.showToast({
	// 		title: '请在小程序中访问，才能正常使用拍照。',
	// 		icon: 'none',
	// 		duration: 2000
	// 	})	
	// }
}

function getWechatMiniImageList(){
	if (!(document.hidden || document.visibilityState === 'hidden')) {
		if("" != wechatMiniChooseImageCallBackFunBatchSeq){
			request({
				url: '/wechat/mini/image/getImageUrls.do',
				method: 'POST',
				data: jsondata
			}).then(res => {
				let data = res.data
				wechatMiniChooseImageCallBackFunBatchSeq = ""
				wechatMiniChooseImageCallBackFun(data.data)
			})
		}
	}
}

function handleVisibilityChange(){	
	try {
	  const listeners = window.getEventListeners(document);
		console.log('listeners', listeners)
		if (listeners && listeners.visibilitychange) {
			console.log('visibilitychange listener exists');
			return true;
		} else {
			console.log('visibilitychange listener does not exist');
			return false;
		}
	} catch (e) {
	  console.log('getEventListeners method is not supported or available.');
		return false;
	}
}